﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;

namespace A_link_Desktop
{
    class LoginModule
    {
        const String URL = "http://127.0.0.1:8080/home/index.php";
        private bool m_loggedIn;

        public bool LoggedIn
        {
            get 
            { 
                return m_loggedIn; 
            }
        }

        public LoginModule()
        {
            Initialize();
        }

        private void Initialize()
        {
            m_loggedIn = false;
        }

        public bool HasInternetConnection()
        {
            // Check if a connection could be made.
            try
            {
                // Create a new WebRequest.
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(URL);
                
                // Set the time out in milli-seconds.
                request.Timeout = 5000;

                // Set the credentials.
                request.Credentials = CredentialCache.DefaultNetworkCredentials;

                // Get the response from the request.
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();

                // Check if the connection was ok.
                if (response.StatusCode == HttpStatusCode.OK)
                    return true;
                return false;
            }
            catch(Exception e)
            {
                // Since an exception was thrown, something went wrong. Just return false.
                return false;
            }
        }
    }
}
